namespace SimpleWebServices.Web
{
    using System.Web;
    using Moq;
    using SimpleWebServices;
    using Xunit;

    public sealed class HttpFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(typeof(Http).IsStatic());
        }

        [Fact]
        public void op_CurrentProtocol_stringEmpty()
        {
            string expected = string.Empty;
            string actual = Http.ToCurrentProtocol(expected);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_CurrentProtocol_stringNull()
        {
            Assert.Null(Http.ToCurrentProtocol(null as string));
        }

        [Fact]
        public void op_CurrentProtocol_string_whenRequestIsSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(true).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "example";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_string_whenRequestIsNotSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(false).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "example";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringHttp_whenRequestIsSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(true).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "https://example.com/";
                string actual = Http.ToCurrentProtocol("http://example.com/");

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringHttp_whenRequestIsNotSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(false).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "http://example.com/";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringHttps_whenRequestIsSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(true).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "https://example.com/";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringHttps_whenRequestIsNotSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(false).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "http://example.com/";
                string actual = Http.ToCurrentProtocol("https://example.com/");

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringUrn_whenRequestIsSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(true).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "urn://example/";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_CurrentProtocol_stringUrn_whenRequestIsNotSecure()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<bool>(x => x.Request.IsSecureConnection).Returns(false).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                string expected = "urn://example/";
                string actual = Http.ToCurrentProtocol(expected);

                Assert.Equal<string>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }
    }
}